package com.battery.system.service.battery.impl;

import com.battery.common.core.text.Convert;
import com.battery.common.enums.AccountTypeEnum;
import com.battery.common.enums.YesOrNoEnum;
import com.battery.common.utils.DateUtils;
import com.battery.system.domain.ProxyUser;
import com.battery.system.domain.VO.PartnerVO;
import com.battery.system.mapper.ProxyUserMapper;
import com.battery.system.service.battery.IProxyUserService;
import com.battery.system.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * 账号Service业务层处理
 *
 * @author battery
 * @date 2021-01-20
 */
@Service
public class ProxyUserServiceImpl extends BaseServiceImpl<ProxyUser> implements IProxyUserService {


    @Autowired
    private ProxyUserMapper proxyUserMapper;

    /**
     * 查询账号
     *
     * @param id 账号ID
     * @return 账号
     */
    @Override
    public ProxyUser selectProxyUserById(String id) {
        return proxyUserMapper.selectProxyUserById(id);
    }

    /**
     * 查询账号列表
     *
     * @param proxyUser 账号
     * @return 账号
     */
    @Override
    public List<ProxyUser> selectProxyUserList(ProxyUser proxyUser) {
        return proxyUserMapper.selectProxyUserList(proxyUser);
    }

    /**
     * 新增账号
     *
     * @param proxyUser 账号
     * @return 结果
     */
    @Override
    public int insertProxyUser(ProxyUser proxyUser) {
        proxyUser.setCreateTime(DateUtils.getNowDate());
        return proxyUserMapper.insertProxyUser(proxyUser);
    }

    /**
     * 修改账号
     *
     * @param proxyUser 账号
     * @return 结果
     */
    @Override
    public int updateProxyUser(ProxyUser proxyUser) {
        proxyUser.setUpdateTime(DateUtils.getNowDate());
        return proxyUserMapper.updateProxyUser(proxyUser);
    }

    /**
     * 删除账号对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteProxyUserByIds(String ids) {
        return proxyUserMapper.deleteProxyUserByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除账号信息
     *
     * @param id 账号ID
     * @return 结果
     */
    @Override
    public int deleteProxyUserById(String id) {
        return proxyUserMapper.deleteProxyUserById(id);
    }

    @Override
    public ProxyUser selectByPhone(String phone, Integer type, String pwd) {
        Example example = new Example(ProxyUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone", phone);
        if (pwd != null) {
            criteria.andEqualTo("password", pwd);
        }
        if (type == 3) {
            criteria.andEqualTo("accountType", type);
        } else if (type == 1 || type == 2) {
            List<Integer> iterable = new ArrayList<>();
            iterable.add(2);
            iterable.add(1);
            criteria.andIn("accountType", iterable);
        }
        criteria.andEqualTo("isDeleted", YesOrNoEnum.NO.getCode());
        return proxyUserMapper.selectOneByExample(example);
    }

    /**
     * 校验手机号是否重复
     *
     * @param phone       手机号
     * @param accountType 账号类型
     * @return
     */
    @Override
    public boolean checkPhoneExist(String phone, Integer accountType) {
        // 合作商不能为员工，但是可以为商户
        Example example = new Example(ProxyUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone", phone);
        criteria.andEqualTo("isDeleted", YesOrNoEnum.NO.getCode());
//        criteria.andEqualTo("isBan", YesOrNoEnum.NO.getCode());
        if (AccountTypeEnum.PROJECT.getCode().intValue() == accountType.intValue()
                || AccountTypeEnum.PARTNER.getCode().intValue() == accountType.intValue()) {
            // 合作商校验
            List<Integer> iterable = new ArrayList<>();
            iterable.add(AccountTypeEnum.PROJECT.getCode());
            iterable.add(AccountTypeEnum.PARTNER.getCode());
            criteria.andIn("accountType", iterable);
//            criteria.andNotEqualTo("accountType", AccountTypeEnum.MERCHANT.getCode());
        } else {
            // 商户不能为员工
            criteria.andEqualTo("accountType", AccountTypeEnum.MERCHANT.getCode());
        }
        return proxyUserMapper.selectCountByExample(example) > 0;
    }


    /**
     * @param accountId
     * @param accountType
     * @return
     */
    @Override
    public boolean checkMainAccount(String accountId, Integer accountType) {
        Example example = new Example(ProxyUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("accountId", accountId);
        criteria.andEqualTo("accountType", accountType);
        criteria.andEqualTo("isDeleted", YesOrNoEnum.NO.getCode());
        criteria.andEqualTo("isStaff", YesOrNoEnum.NO.getCode());
        return proxyUserMapper.selectCountByExample(example) > 0;
    }


    /**
     * @param phone       登陆账号（手机号）
     * @param accountType 账号类型
     * @return
     */
    @Override
    public ProxyUser selectOneByAccount(String phone, Integer accountType) {
        // 合作商不能为员工，但是可以为商户
        Example example = new Example(ProxyUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone", phone);
        criteria.andEqualTo("isDeleted", YesOrNoEnum.NO.getCode());
//        criteria.andEqualTo("isBan", YesOrNoEnum.NO.getCode());
        if (AccountTypeEnum.PROJECT.getCode().intValue() == accountType.intValue()
                || AccountTypeEnum.PARTNER.getCode().intValue() == accountType.intValue()) {
            // 合作商校验
//            criteria.andNotEqualTo("accountType", AccountTypeEnum.PARTNER.getCode());
            List<Integer> iterable = new ArrayList<>();
            iterable.add(AccountTypeEnum.PROJECT.getCode());
            iterable.add(AccountTypeEnum.PARTNER.getCode());
            criteria.andIn("accountType", iterable);
        } else {
            // 商户不能为员工
            criteria.andEqualTo("accountType", AccountTypeEnum.MERCHANT.getCode());
        }
        example.orderBy("createTime").desc();
        List<ProxyUser> list = proxyUserMapper.selectByExample(example);
        return list != null && list.size() > 0 ? list.get(0) : null;
    }

    /**
     * 根据账号ID 以及账号类型获取对应的账号信息 (排除员工)
     *
     * @param accountId
     * @param accountType
     * @return
     */
    @Override
    public ProxyUser selectByAccountId(String accountId, Integer accountType) {
        Example example = new Example(ProxyUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("accountType", accountType);
        criteria.andEqualTo("accountId", accountId);
        criteria.andEqualTo("isDeleted", YesOrNoEnum.NO.getCode());
        criteria.andEqualTo("isStaff", YesOrNoEnum.NO.getCode());
        return proxyUserMapper.selectOneByExample(example);
    }
}
